using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESecretary.Business;
using ESecretary.DataEntity;
using System.Net;
using System.Web.Services.Protocols;
using ESecretary.MyCommon;
using ESecretary.OA.Service;

namespace ESecretary.WinUI
{
    public partial class FrmAddUser : Form
    {
        public event EventHandler UserSelected;
        private static OADelegate service = OADelegate.Create();
        private List<int> lstInMeetingIDs;

        public FrmAddUser(List<int> lstInMeetingIDs)
        {
            this.lstInMeetingIDs = lstInMeetingIDs;
            InitializeComponent();
        }

        private void FrmAddUser_Load(object sender, EventArgs e)
        {
            try
            {
                List<OnlineUserInfo> dt = service.GetOnlineUserList();
                lvUser.Items.Clear();
                foreach (OnlineUserInfo dr in dt)
                {
                    if (lstInMeetingIDs.IndexOf(dr.ID) < 0)
                    {
                        string strUserName = dr.Name;
                        string strDeptName = dr.DeptName;

                        ListViewItem item = new ListViewItem(strUserName);
                        item.SubItems.Add(strDeptName);
                        item.Tag = dr.ID + "|" + dr.IP;

                        lvUser.Items.Add(item);
                    }
                }
            }
            catch (WebException)
            {}
            catch (SoapException) { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetType().Name);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            List<ChatUserInfo> users = new List<ChatUserInfo>();
            foreach (ListViewItem item in lvUser.Items)
            {
                if (item.Checked)
                {
                    string strTag = item.Tag.ToString();
                    int userID = int.Parse(strTag.Substring(0, strTag.IndexOf('|')));
                    string userName = item.SubItems[0].Text;
                    string ip = strTag.Substring(strTag.IndexOf('|') + 1);

                    users.Add(new ChatUserInfo(userID, userName, ip));       
                }
            }                
            UserSelected(users, null);           
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        
    }
}